<template>
  <div class="article-container">
    <!-- 导航栏 -->
    <van-nav-bar fixed
                 left-arrow
                 @click-left="$router.back()"
                 title="文章详情"></van-nav-bar>
    <!-- /导航栏 -->

    <!-- 加载中 loading -->
    <van-loading class="article-loading"
                 v-if="loading" />
    <!-- /加载中 loading -->

    <!-- 文章详情 -->
    <div class="detail"
         v-else>
      <h3 class="title">{{article.title}}</h3>
      <div class="author">
        <van-image round
                   width="1rem"
                   height="1rem"
                   fit="fill"
                   :src="article.aut_photo" />
        <div class="text">
          <p class="name">{{article.aut_name}}</p>
          <p class="time">{{article.pubdate | handlerRelativeTime}}</p>
        </div>

        <van-button round
                    v-if="article.is_followed"
                    size="small"
                    type="danger"
                    @click="handlerFollow">取 关</van-button>
        <van-button round
                    v-else
                    size="small"
                    type="info"
                    @click="handlerFollow">+关注</van-button>
      </div>
      <div class="content">
        <div v-html="article.content"></div>
      </div>
      <van-divider>正文结束啦</van-divider>
      <div class="zan">
        <van-button round
                    size="small"
                    hairline
                    type="primary"
                    plain
                    icon="good-job-o">点赞</van-button>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button round
                    size="small"
                    hairline
                    type="danger"
                    plain
                    icon="delete">不喜欢</van-button>
      </div>
    </div>
    <!-- /文章详情 -->
    <!-- 文章评论列表 -->
    <article-comments :id="id"></article-comments>
    <!-- /文章评论列表 -->
  </div>
</template>

<script>
// 导入获取文章详情接口
import { reqGetArticleDetail } from '@/api/articles'
import { reqFollowUser, reqUnFollowUser } from '@/api/user'
import ArticleComments from './components/ArticleComments'
export default {
  name: 'ArticleIndex',
  props: {
    // id接收的是路由中的参数文章id
    id: {
      type: String,
      required: true
    }
  },
  components: {
    ArticleComments
  },
  data () {
    return {
      loading: true, // 控制加载中的 loading 状态
      article: {}
    }
  },
  async created () {
    // console.log(this.$route.params.id)
    // console.log(this.id)
    const res = await reqGetArticleDetail(this.id)
    console.log(res)
    // 将返回的文章详情对象保存在data中article
    this.article = res.data
    // 请求结束之后将loading设置为false 显示正文, 隐藏加载中状态
    this.loading = false
  },
  methods: {
    // 关注和取关的方法
    async handlerFollow () {
      // 基于article.is_followed 的状态 发送不同的请求
      if (this.article.is_followed) {
        // 取消关注
        await reqUnFollowUser(this.article.aut_id) // data:''  取关之后返回的就是一个空字符串
        this.$toast('取关成功')
      } else {
        // 添加关注
        await reqFollowUser(this.article.aut_id)
        this.$toast('关注成功')
      }
      // 可以在关注或取关之后重新获取一个当前文章的详情
      // 直接将本地的数据修改一下 取反
      this.article.is_followed = !this.article.is_followed
    }
  }
}
</script>
<style lang='scss' scoped>
.article-container {
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
}
.article-loading {
  padding-top: 100px;
  text-align: center;
}
.error {
  padding-top: 100px;
  text-align: center;
}
.detail {
  padding: 50px 10px;
  .title {
    font-size: 16px;
  }
  .zan {
    text-align: center;
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.3;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    font-size: 14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    ::v-deep img {
      max-width: 100%;
      background: #f9f9f9;
    }
  }
}
</style>
